<?php
namespace Api\Service;

/**
 * 我的收藏
 */
class CollectService
{
    /**
     * 收藏的商品
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function goods($user_id)
    {
        $goods = M('collect')->alias('c')
            ->field('g.id,g.title,g.image,g.description,g.price,g.market_price,g.is_delete')
            ->join('__GOODS__ g on c.refer_id = g.id and g.is_delete = 0')
            ->where([
                'c.refer_type' => 1,
                'c.is_delete'  => 0,
            ])->order('id desc')
            ->select();
        return $goods ? $goods : [];
    }

    /**
     * 收藏的店铺
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function shop($user_id)
    {
        $shops = M('collect')->alias('c')
            ->field('s.id,s.name,s.logo,s.is_open')
            ->join('__SHOP__ s on c.refer_id = s.id and s.is_open = 1')
            ->where([
                'c.refer_type' => 2,
                'c.is_delete'  => 0,
            ])->order('id desc')
            ->select();

        foreach ($shops as &$shop) {
            $shop['news']  = D('Api/Goods', 'Service')->getNewCountByShopId($shop['id']);
            $shop['goods'] = D('Api/Goods', 'Service')->getGoods($shop['id']);
        }

        return $shops ? $shops : [];
    }
}
